//
//  UserEditAgeController.m
//  CheekiOS
//
//  Created by mac on 2017/6/17.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserEditAgeController.h"
#import "KSDatePicker.h"

@interface UserEditAgeController ()<UITableViewDelegate ,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *AgeLab;
@property (nonatomic, strong) UILabel *AgeNumberLab;

@property (nonatomic, strong) KSDatePicker *picker;

@property (nonatomic, strong) UILabel *constellationLab;
@property (nonatomic, strong) UILabel *constellationStrLab;


@property (nonatomic, strong) NSString *stringConMon;
@property (nonatomic, strong) NSString *stringConDay;
@property (nonatomic, strong) NSString *stringConYear;
@property (nonatomic, strong) NSString *stringAge;

@end

@implementation UserEditAgeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改年龄";
    _stringAge =@"";
    UIButton *RightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [RightBtn addTarget:self action:@selector(RightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [RightBtn setTitle:@"保存" forState:UIControlStateNormal];
    RightBtn.frame = CGRectMake(0, 0, 40, 40);
    [RightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    RightBtn.titleLabel.font = FontP(15);
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:RightBtn];
    self.navigationItem.rightBarButtonItem = leftItem;


    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:self.tableView];
    
}

- (void)RightBtnAction{
    NSDictionary *dict = @{
                           @"birthday":_stringAge,
                           @"uid":[HQHttpManager getUserId]
                           };
    [self updateUser:dict];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        static NSString *EditAgeCellIdent = @"EditAgeCellIdent";
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:EditAgeCellIdent];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        _AgeLab =[UILabel new];
        _AgeLab.font = FontBold(15);
        _AgeLab.text = @"年龄";
        _AgeLab.textAlignment = NSTextAlignmentLeft;
        _AgeLab.frame = CGRectMake(15, 0, 100, 55);
        
        
        _AgeNumberLab =[UILabel new];
        _AgeNumberLab.font = FontBold(15);
        _AgeNumberLab.text = _ageStr;
        _AgeNumberLab.textAlignment = NSTextAlignmentRight;
        _AgeNumberLab.frame = CGRectMake(KScreenWidth-135*SCREEN_WIDTH_RATIO47, 0, 100, 55);
        _AgeNumberLab.textColor  = RGB(175, 177, 180);

        [cell addSubview:_AgeLab];
        [cell addSubview:_AgeNumberLab];
        return cell;
    }else{
    
        static NSString *EditAgeconstellationCellIdent = @"EditAgeconstellationCellIdent";
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:EditAgeconstellationCellIdent];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        _constellationLab =[UILabel new];
        _constellationLab.font = FontBold(15);
        _constellationLab.text = @"星座";
        _constellationLab.textAlignment = NSTextAlignmentLeft;
        _constellationLab.frame = CGRectMake(15, 0, 100, 55);
        
        
        _constellationStrLab =[UILabel new];
        _constellationStrLab.font = FontBold(15);
       _constellationStrLab.textColor  = RGB(175, 177, 180);
        _constellationStrLab.text = _constellationStr;
        _constellationStrLab.textAlignment = NSTextAlignmentRight;
        _constellationStrLab.frame = CGRectMake(KScreenWidth-120*SCREEN_WIDTH_RATIO47, 0, 100, 55);
        
        [cell addSubview:_constellationLab];
        [cell addSubview:_constellationStrLab];
        return cell;

    }
    
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        [self buttonClick];
    }
    
}

- (void)buttonClick{
    WEAKSELF
    //x,y 值无效，默认是居中的
    _picker = [[KSDatePicker alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 40, KScreenHeight/2)];
    
    //配置中心，详情见KSDatePikcerApperance

    _picker.appearance.radius = 5;
    
    //设置回调
    _picker.appearance.resultCallBack = ^void(KSDatePicker* datePicker,NSDate* currentDate,KSDatePickerButtonType buttonType){
        
        if (buttonType == KSDatePickerButtonCommit) {
            
            

            
            NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd"];
            NSString *dataStr =  [formatter stringFromDate:currentDate];
            
            
            
            //判断是否大于等于18岁
            NSDate *DcurrentDate = [NSDate date];
            NSDateFormatter *Dformatter = [[NSDateFormatter alloc] init];
            [Dformatter setDateFormat:@"yyyy-MM-dd"];
            NSString *DdataStr = [Dformatter stringFromDate:DcurrentDate];
            
            //星座判断时间截取
            
            
            weakSelf.stringConMon = [dataStr substringWithRange:NSMakeRange(5,2)];
            weakSelf.stringConDay = [dataStr substringWithRange:NSMakeRange(8, 2)];
            weakSelf.stringConYear = [dataStr substringWithRange:NSMakeRange(0, 4)];
            NSLog(@"截取的值为：%@  ==%@",weakSelf.stringConMon,weakSelf.stringConDay);
            NSString *strYear = [DdataStr substringWithRange:NSMakeRange(0, 4)];
            
            NSInteger conYear = [weakSelf.stringConYear integerValue];
            NSInteger strYearinter = [strYear integerValue];
            NSInteger ageInteger = strYearinter-conYear;
            NSString *strAge = [NSString stringWithFormat:@"%ld",(long)ageInteger];
            
            
            NSCalendar *calendar = [NSCalendar currentCalendar];
            unsigned int unitFlags = NSCalendarUnitYear;
            NSDateComponents *date = [calendar components:unitFlags fromDate:currentDate toDate:DcurrentDate options:0];
            
            if((long)[date year]>=18){ 
                _ageStr = strAge;
                _stringAge = dataStr;
                [weakSelf getAstroWithMonth:[weakSelf.stringConMon intValue] day:[weakSelf.stringConDay intValue]];
                [weakSelf.tableView reloadData];
            }else{
                [weakSelf dateDayMonAlerter];
            }
        }
    };
    // 显示
    [_picker show];
    
}



- (void)dateDayMonAlerter{

    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"年龄要大于18岁" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:confirAction];
    [self.navigationController presentViewController:alertVC animated:YES completion:nil];

}

-(NSString *)getAstroWithMonth:(int)m day:(int)d{
    
    NSString *astroString = @"摩羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手摩羯";
    NSString *astroFormat = @"102123444543";
    NSString *result;
    
    if (m<1||m>12||d<1||d>31){
        return @"错误日期格式!";
    }
    
    if(m==2 && d>29)
    {
        return @"错误日期格式!!";
    }else if(m==4 || m==6 || m==9 || m==11) {
        
        if (d>30) {
            return @"错误日期格式!!!";
        }
    }
    
    result=[NSString stringWithFormat:@"%@",[astroString substringWithRange:NSMakeRange(m*2-(d < [[astroFormat substringWithRange:NSMakeRange((m-1), 1)] intValue] - (-19))*2,2)]];
    
    _constellationStr = result;
    
    
   
    
    return _constellationStr = [NSString stringWithFormat:@"%@座",result];
    
    
}

//头像传给服务器
-(void)updateUser:(NSDictionary *)dict{
    [HcardHttpmanager MakePersionalInfoWithParam:dict success:^(id sucess) {
        if(sucess){
            NSString *UserEditNameUpdateNotifiction = @"UserEditNameUpdateNotifiction";
            [[NSNotificationCenter defaultCenter] postNotificationName:UserEditNameUpdateNotifiction object:nil];
            
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(NSError *error) {
        
    }];
}

@end
